/* eslint-disable */
/**
 * @file io，loader相关输入输出
 * @author wangqun@baidu.com
 */

export default class io {
    constructor() {
        this.fromPixels2DContext = document.createElement('canvas').getContext('2d');
    };

    fromPixels(pixels, opt) {
        pixels = pixels.input;
        const shape = opt[0].shape;
        const numChannels = opt[0].shape[0];
        if (pixels == null) {
            throw new Error(
                'pixels passed to tf.browser.fromPixels() can not be null');
        }
        let vals;
        // tslint:disable-next-line:no-any
        // tslint:disable-next-line:no-any
        if (pixels.getContext != null) {
            // tslint:disable-next-line:no-any
            vals = pixels
                .getContext('2d')
                .getImageData(0, 0, pixels.width, pixels.height)
                .data;
        } else if (pixels instanceof ImageData) {
            vals = pixels.data;
        } else if (
            pixels instanceof HTMLImageElement ||
            pixels instanceof HTMLVideoElement) {
            if (this.fromPixels2DContext == null) {
                throw new Error(
                    'Can\'t read pixels from HTMLImageElement outside ' +
                    'the browser.');
            }
            this.fromPixels2DContext.canvas.width = pixels.width;
            this.fromPixels2DContext.canvas.height = pixels.height;
            this.fromPixels2DContext.drawImage(
                pixels, 0, 0, pixels.width, pixels.height);
            vals = this.fromPixels2DContext
                .getImageData(0, 0, pixels.width, pixels.height)
                .data;
        } else {

        }
        let values;
        if (numChannels === 4) {
            values = new Array(vals);
        } else {
            const numPixels = (shape[1] || pixels.width) * (shape[2] ||pixels.height);
            // console.log(numPixels, numPixels * numChannels);
            values = new Array(numPixels * numChannels);
            for (let i = 0; i < numPixels; i++) {
                for (let channel = 0; channel < numChannels; ++channel) {
                    values[i * numChannels + channel] = vals[i * 4 + channel];
                }
            }
        }
        // console.log(pixels.height, pixels.width, numChannels, values);
        // const outShape: [number, number, number] =
        //     [pixels.height, pixels.width, numChannels];
        values = [
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            7.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            6.0,
            7.0,
            0.0,
            0.0,
            0.0,
            0.0,
            3.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            3.0,
            0.0,
            0.0,
            14.0,
            16.0,
            8.0,
            1.0,
            0.0,
            0.0,
            0.0,
            14.0,
            1.0,
            0.0,
            0.0,
            14.0,
            4.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            5.0,
            13.0,
            0.0,
            0.0,
            0.0,
            9.0,
            0.0,
            27.0,
            0.0,
            0.0,
            0.0,
            5.0,
            0.0,
            0.0,
            3.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            4.0,
            0.0,
            0.0,
            5.0,
            11.0,
            5.0,
            4.0,
            8.0,
            0.0,
            0.0,
            15.0,
            7.0,
            0.0,
            2.0,
            7.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            11.0,
            2.0,
            0.0,
            0.0,
            0.0,
            0.0,
            4.0,
            11.0,
            3.0,
            0.0,
            2.0,
            0.0,
            5.0,
            3.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            2.0,
            0.0,
            0.0,
            10.0,
            6.0,
            0.0,
            0.0,
            0.0,
            0.0,
            4.0,
            9.0,
            0.0,
            0.0,
            2.0,
            3.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            8.0,
            0.0,
            8.0,
            11.0,
            0.0,
            4.0,
            113.0,
            202.0,
            249.0,
            255.0,
            255.0,
            135.0,
            44.0,
            0.0,
            7.0,
            3.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            2.0,
            0.0,
            2.0,
            0.0,
            33.0,
            188.0,
            230.0,
            101.0,
            52.0,
            6.0,
            106.0,
            162.0,
            183.0,
            11.0,
            0.0,
            4.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            9.0,
            0.0,
            4.0,
            58.0,
            230.0,
            189.0,
            31.0,
            0.0,
            3.0,
            0.0,
            14.0,
            0.0,
            204.0,
            17.0,
            7.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            20.0,
            24.0,
            231.0,
            181.0,
            0.0,
            0.0,
            5.0,
            4.0,
            2.0,
            0.0,
            119.0,
            228.0,
            0.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            173.0,
            232.0,
            32.0,
            4.0,
            10.0,
            0.0,
            0.0,
            7.0,
            79.0,
            230.0,
            108.0,
            18.0,
            0.0,
            10.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            2.0,
            100.0,
            246.0,
            47.0,
            0.0,
            5.0,
            0.0,
            1.0,
            8.0,
            63.0,
            216.0,
            109.0,
            0.0,
            0.0,
            6.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            8.0,
            122.0,
            210.0,
            0.0,
            31.0,
            0.0,
            8.0,
            28.0,
            109.0,
            235.0,
            182.0,
            0.0,
            13.0,
            0.0,
            22.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            128.0,
            233.0,
            0.0,
            6.0,
            66.0,
            126.0,
            180.0,
            191.0,
            220.0,
            27.0,
            0.0,
            0.0,
            11.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            78.0,
            246.0,
            233.0,
            220.0,
            255.0,
            199.0,
            59.0,
            235.0,
            68.0,
            12.0,
            0.0,
            1.0,
            2.0,
            1.0,
            10.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            2.0,
            0.0,
            80.0,
            120.0,
            139.0,
            62.0,
            0.0,
            155.0,
            211.0,
            5.0,
            10.0,
            0.0,
            0.0,
            0.0,
            3.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            5.0,
            2.0,
            0.0,
            0.0,
            90.0,
            255.0,
            70.0,
            0.0,
            0.0,
            0.0,
            9.0,
            0.0,
            0.0,
            9.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            17.0,
            5.0,
            0.0,
            11.0,
            47.0,
            227.0,
            159.0,
            0.0,
            0.0,
            8.0,
            0.0,
            0.0,
            2.0,
            6.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            5.0,
            0.0,
            0.0,
            0.0,
            4.0,
            213.0,
            207.0,
            19.0,
            0.0,
            0.0,
            3.0,
            12.0,
            0.0,
            2.0,
            4.0,
            2.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            16.0,
            7.0,
            91.0,
            253.0,
            50.0,
            0.0,
            0.0,
            4.0,
            0.0,
            2.0,
            0.0,
            1.0,
            2.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            2.0,
            5.0,
            0.0,
            45.0,
            252.0,
            131.0,
            0.0,
            8.0,
            0.0,
            7.0,
            0.0,
            15.0,
            5.0,
            0.0,
            0.0,
            2.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            8.0,
            11.0,
            207.0,
            205.0,
            30.0,
            2.0,
            0.0,
            0.0,
            22.0,
            0.0,
            0.0,
            4.0,
            9.0,
            11.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            14.0,
            155.0,
            255.0,
            28.0,
            0.0,
            0.0,
            6.0,
            4.0,
            0.0,
            5.0,
            150.0,
            210.0,
            91.0,
            17.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            14.0,
            40.0,
            250.0,
            91.0,
            0.0,
            0.0,
            7.0,
            0.0,
            0.0,
            24.0,
            0.0,
            10.0,
            130.0,
            183.0,
            147.0,
            11.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            207.0,
            146.0,
            4.0,
            0.0,
            4.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            25.0,
            237.0,
            29.0,
            0.0,
            12.0,
            0.0,
            0.0,
            14.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            13.0,
            0.0,
            15.0,
            7.0,
            0.0,
            9.0,
            2.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            4.0,
            0.0,
            4.0,
            3.0,
            4.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0
        ];
        return [{data: values, shape: shape, name: 'pixel'}];
    }
}
/* eslint-enable */
